# 检查资源是否足够, 如果不足则阻塞无限循环休眠检查资源, 资源足够时取消阻塞
from os.path import join
from sys import stderr
from const import RESOURCE_PATH, PYLTP_MODEL_PATH
from time import sleep

resource = [
    join(PYLTP_MODEL_PATH, 'cws.model'),
    join(PYLTP_MODEL_PATH, 'ner.model'),
    join(PYLTP_MODEL_PATH, 'parser.model'),
    join(PYLTP_MODEL_PATH, 'pisrl.model'),
    join(PYLTP_MODEL_PATH, 'pos.model'),
    join(RESOURCE_PATH, 'multiple', 'deposit.aiml'),
    join(RESOURCE_PATH, 'multiple', 'recommend.aiml'),
    join(RESOURCE_PATH, 'sensitiveness', 'keyword.txt'),
    join(RESOURCE_PATH, '1.png'),
    join(RESOURCE_PATH, '2.png'),
    join(RESOURCE_PATH, 'business.aiml'),
    join(RESOURCE_PATH, 'cilin.txt'),
    join(RESOURCE_PATH, 'condition.aiml'),
    join(RESOURCE_PATH, 'contain_template.aiml'),
    join(RESOURCE_PATH, 'guotu_dict.txt'),
    join(RESOURCE_PATH, 'information.aiml'),
    join(RESOURCE_PATH, 'navi_template.aiml'),
    join(RESOURCE_PATH, 'neo4j.xlsx'),
    join(RESOURCE_PATH, 'neo4j2.xlsx'),
    join(RESOURCE_PATH, 'pair_for_banzheng.txt'),
    join(RESOURCE_PATH, 'pair_for_jieyue.txt'),
    join(RESOURCE_PATH, 'pair_for_reading_qa.txt'),
    join(RESOURCE_PATH, 'pattern_for_cyclopedia.aiml'),
    join(RESOURCE_PATH, 'stopword.txt'),
    join(RESOURCE_PATH, 'template.aiml'),
    join(RESOURCE_PATH, 'main-engine.yml'),
]

# 检查间隔时间(秒)
internal = 10


def check_resource():
    each = ''
    while True:
        print('resource checking')
        try:
            for each in resource:
                file = open(each, 'r')
                file.close()
            break
        except FileNotFoundError:
            stderr.write('resource missing: %s, check again later\n' % each)
            stderr.flush()
            sleep(internal)
    print('resource check completed')
